<template>
	<div>
		<!-- 搜索结果页-头部导航 -->
		<div class="search-result-container">
			<!-- 点击实现后退效果 -->
			<van-nav-bar title="搜索结果" left-arrow @click-left="$router.back()" fixed />
			<!-- 搜索结果 -->
			<div>
				<van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad"
					:immediate-check="false" offset="50">
					<ArticleItem v-for="(item, index) in articleList" :key="index" :artObj="item"
					@click.native="$router.push(`/article_detail?aid=${item.art_id}`)"/>
				</van-list>
			</div>
		</div>
	</div>
</template>

<script>
import { searchResultListAPI } from '@/api'
import ArticleItem from '@/components/ArticleItem.vue'
export default {
  name: 'SearchResult',
  components: {
    ArticleItem
  },
  data () {
    return {
      articleList: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await searchResultListAPI({ page: this.page, q: this.$route.params.keywords })
      if (res.data.data.results.length === 0) {
        this.finished = true
        return
      }
      this.articleList = [...this.articleList, ...res.data.data.results]
      this.loading = false
    },
    onLoad () {
      if (this.articleList.length > 0) {
        this.page++
        this.getData(this.page)
      }
    }
  }
}
</script>

<style lang="less" scoped>
::v-deep .van-icon {
	color: white
}

::v-deep .van-ellipsis {
	color: white
}

.search-result-container {
	padding-top: 46px;
	color: white
}

.van-nav-bar {
	background-color: #007bff;
}
</style>
